package io.github.biezhi.elves.spider;

import java.net.SocketTimeoutException;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import io.github.biezhi.elves.ok.http.FastHttpClient;
import io.github.biezhi.elves.qcc.model.CompBidDetail;
import io.github.biezhi.elves.qcc.model.CompBidUrlDate;
import io.github.biezhi.elves.qcc.model.CompBidding;
import io.github.biezhi.elves.service.QccBidDetailService;
import io.github.biezhi.elves.thread.QccRunnable;
import io.github.biezhi.elves.utils.StringUtils;

 /**
  * 爬取企查查公告原文链接，公告时间和网页全部内容
  * @author Administrator
  *
  */
@Component
public class QccBidDetailSpider {
	
	@Autowired
	private QccBidDetailService qccBidDetailService;
	
	
	
	//公告基本信息到数据库
	public void bidDetailSpiderToDB() throws Exception{
		
		//查询公告基本信息
		List<CompBidUrlDate> queryCompBidUrlDateList = qccBidDetailService.queryCompBidUrlDate(10000);
		
		for (CompBidUrlDate compBidUrlDate : queryCompBidUrlDateList) {
			//公告id
			Long bidId = compBidUrlDate.getBidId();
			//企业id
			Long compId = compBidUrlDate.getCompId();
			//企业名称
			String compName = compBidUrlDate.getCompName();
			//公告标题
			String biddingTitle = compBidUrlDate.getBiddingTitle();
			//公告发表日期
/*			String biddingDate = compBidUrlDate.getBiddingDate();
			if (biddingDate != null && !"".equals(biddingDate.trim())) {
				biddingDate = biddingDate.replace("发表于 ", "");
			}*/
			
			//根据公告标题url爬取页面
			String biddingHtml = "";
			//公告在企查查的基本信息
			CompBidDetail compBidDetail = new CompBidDetail();
			
			//招投标标题的链接
			String biddingDetailUrl = compBidUrlDate.getBiddingDetailUrl();
			if (biddingDetailUrl.contains("caigou.chinatelecom.com.cn")) {
				biddingHtml = spiderQccBidUrl(biddingTitle);
				//解析获取参数 id和encryCode
				Document doc = Jsoup.parse(biddingHtml);
				String html1= doc.getElementsByClass("table_data").html();
				try {
					String str = html1.substring(html1.indexOf("view('"), html1.indexOf("')\">"));
					String idHtml ="";
					String encryCode ="";
					String[] strs = str.split("','");
					for(int i=0;i<strs.length;i++){
						idHtml = strs[0].toString();
						encryCode = strs[1].toString();
					}
					String id = idHtml.substring(6);
					System.out.println(id);
					System.out.println(encryCode);
					
					//公告在阳光采购网的的搜索信息爬去公告
					String ygBiddingHtml = spiderYgBidUrl(id,encryCode);
					//插入到数据库的结果表中 
//					compBidDetail.setBiddingDate(biddingDate);
					compBidDetail.setBiddingTitle(biddingTitle);
					compBidDetail.setBidId(bidId);
					compBidDetail.setCompId(compId);
					compBidDetail.setCompName(compName);
					compBidDetail.setBiddingHtml(ygBiddingHtml);
					compBidDetail.setBiddingDetailUrl(biddingDetailUrl);
					qccBidDetailService.insertCompBidDetail(compBidDetail);
					
					//更新为已爬取
					qccBidDetailService.updateCompBidUrlDateState(bidId, 1);
				} catch (Exception e) {
					// 如果出现数组下标越界异常，则表示查不到该项目，将状态更新为2  异常处理
					qccBidDetailService.updateCompBidUrlDateState(bidId, 2);
				}
				
			} else if (biddingDetailUrl.contains("www.cuecp.cn")) {
				String notice_type1 = "招标公告";
				String notice_type2 = "结果公告";
				String notice_type3 = "单一来源采购公示";
				
				String notice_type = "";
				String id = "";
				
				System.out.println("biddingTitle:" + biddingTitle);
				//根据公告标题url爬取页面
				String qccBiddingHtml = spiderQccBidUrl(biddingTitle,notice_type1);
				
//				System.out.println("返回结果：" + qccBiddingHtml);
				
				//解析获取公告发表时间，以及公告源地址
				id = parseHtml(qccBiddingHtml,notice_type1);
				notice_type = notice_type1;
				
				if(StringUtils.isNullOrEmpty(id)) {
					qccBiddingHtml = spiderQccBidUrl(biddingTitle,notice_type2);
					id = parseHtml(qccBiddingHtml,notice_type2);
					notice_type = notice_type2;
				}
			
				if(StringUtils.isNullOrEmpty(id)) {
					qccBiddingHtml = spiderQccBidUrl(biddingTitle,notice_type3);
					id = parseHtml(qccBiddingHtml,notice_type3);
					notice_type = notice_type3;
				}
			
				System.out.println("id=" + id + "notice_type=" + notice_type);
				
				//根据公告标题url爬取公告
				if(!StringUtils.isNullOrEmpty(id)) {
					qccBiddingHtml = spiderQccBidNotice(notice_type,id);
//					System.out.println("返回结果：" + qccBiddingHtml);
					
					//公告在企查查的基本信息
//					compBidDetail.setBiddingDate(biddingDate);
					compBidDetail.setBiddingTitle(biddingTitle);
					compBidDetail.setBidId(bidId);
					compBidDetail.setCompId(compId);
					compBidDetail.setCompName(compName);
					compBidDetail.setBiddingHtml(qccBiddingHtml);
					compBidDetail.setBiddingDetailUrl(biddingDetailUrl);
					qccBidDetailService.insertCompBidDetail(compBidDetail);
					
					//更新为已爬取
					qccBidDetailService.updateCompBidUrlDateState(bidId, 1);
				}else {
					//更新为爬取异常
					qccBidDetailService.updateCompBidUrlDateState(bidId, 2);
				}
			} else{	
				try {
					//除了以上网站，直接爬取
					biddingHtml = spiderDetailUrlUrl(biddingDetailUrl,bidId);
//					compBidDetail.setBiddingDate(biddingDate);
					compBidDetail.setBiddingTitle(biddingTitle);
					compBidDetail.setBidId(bidId);
					compBidDetail.setCompId(compId);
					compBidDetail.setCompName(compName);
					compBidDetail.setBiddingHtml(biddingHtml);
					compBidDetail.setBiddingDetailUrl(biddingDetailUrl);
					//插入到数据库
					qccBidDetailService.insertCompBidDetail(compBidDetail);
					
					//更新为已爬取
					qccBidDetailService.updateCompBidUrlDateState(bidId, 1);
				} catch (Exception e) {
					//更新为爬取异常
					qccBidDetailService.updateCompBidUrlDateState(bidId, 1);
				}

			}
		}
		
		System.out.println("爬取完成！");
		
	}
	
	//根据公告标题url爬取页面
	private String spiderDetailUrlUrl(String biddingDetailUrl,Long bidId){
		
		String result = "";
		try {
			io.github.biezhi.elves.ok.http.Response okHttpResponse;
			okHttpResponse = FastHttpClient
					.newBuilder()
					.build()
					.get()
					.url(biddingDetailUrl)
					.build()
					.execute();
			System.out.println(biddingDetailUrl);
			result = okHttpResponse.string();
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println("===异常，更新为爬取异常");
			//更新为爬取异常
			qccBidDetailService.updateCompBidUrlDateState(bidId, 2);
		}
		return result;
		
	}
	
	//根据阳光采购网公告标题url爬取页面
	public  String spiderQccBidUrl(String biddingTitle){
		
		String result = "";
		try {
			io.github.biezhi.elves.ok.http.Response okHttpResponse;
			okHttpResponse = FastHttpClient
					.newBuilder()
					.build()
					.post()
					.url("https://caigou.chinatelecom.com.cn/MSS-PORTAL/resultannounc/listForAd.do")
					.addParams("provinceJT", "NJT")
					.addParams("resultAnnounceName", biddingTitle)
					.build()
					.execute();
			System.out.println("公告标题："+biddingTitle);
			result = okHttpResponse.string();
//			if (result.contains("<script>window.location.href='https://www.qichacha.com/")) {
//				System.err.println("爬取过于频繁！" + Thread.currentThread().getName());
//				result = spiderQccBidUrl(biddingTitle);
//			}
			if(!StringUtils.isNullOrEmpty(result)){
				return result;
			}
		} catch (SocketTimeoutException e) {
			System.err.println("===连接超时，重新调用spiderQccBidUrl(biddingTitleUrl)方法===");
			result = spiderQccBidUrl(biddingTitle);
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println("===其他异常，重新调用spiderQccBidUrl(biddingTitleUrl)方法===");
			result = spiderQccBidUrl(biddingTitle);
		}
		return result;
		
	}
	
	//根据阳光采购网公告标题url爬取公告
		public  String spiderYgBidUrl(String id,String encryCode){
			
			String result = "";
			try {
				io.github.biezhi.elves.ok.http.Response okHttpResponse;
				okHttpResponse = FastHttpClient
						.newBuilder()
						.build()
						.get()
						.url("https://caigou.chinatelecom.com.cn/MSS-PORTAL/resultannounc/viewHome.do?id="+id+"&encryCode="+encryCode)
						.build()
						.execute();
				result = okHttpResponse.string();
				if(!StringUtils.isNullOrEmpty(result)){
					return result;
				}
			} catch (SocketTimeoutException e) {
				System.err.println("===连接超时，重新调用spiderYgBidUrl(String id,String encryCode)方法===");
				result = spiderYgBidUrl(id,encryCode);
			} catch (Exception e) {
				e.printStackTrace();
				System.err.println("===其他异常，重新调用spiderYgBidUrl(String id,String encryCode)方法===");
				result = spiderYgBidUrl(id,encryCode);
			}
			return result;
			
		}
		
		//根据公告标题url爬取页面
		private String spiderQccBidUrl(String biddingTitle,String notice_type){
			
			String result = "";
			try {
				io.github.biezhi.elves.ok.http.Response okHttpResponse;
				okHttpResponse = FastHttpClient
						.newBuilder()
						.build()
						.post()
						.url("https://www.cuecp.cn/PAR_PO_WW/indexController.jhtml?cmd=queryNoticeList&")
						.addParams("notice_group", "PUR")
						.addParams("notice_type", notice_type)
						.addParams("notice_title", biddingTitle)
						.build()
						.execute();
				
				result = okHttpResponse.string();
				if (result.contains("<script>window.location.href='https://www.cuecp.cn/")) {
					System.err.println("爬取过于频繁！" + Thread.currentThread().getName());
					result = spiderQccBidUrl(biddingTitle,notice_type);
				}
				if(!StringUtils.isNullOrEmpty(result)){
					return result;
				}
			} catch (SocketTimeoutException e) {
				System.err.println("===连接超时，重新调用spiderQccBidUrl(biddingTitleUrl)方法===");
				result = spiderQccBidUrl(biddingTitle,notice_type);
			} catch (Exception e) {
				e.printStackTrace();
				System.err.println("===其他异常，重新调用spiderQccBidUrl(biddingTitleUrl)方法===");
				result = spiderQccBidUrl(biddingTitle,notice_type);
			}
			return result;
			
		}
		
		//根据公告标题url爬取公告
		private String spiderQccBidNotice(String notice_type,String id){
			
			String result = "";
			try {
				io.github.biezhi.elves.ok.http.Response okHttpResponse;
				okHttpResponse = FastHttpClient
						.newBuilder()
						.build()
						.post()
						.url("https://www.cuecp.cn/PAR_PO_WW/indexController.jhtml?cmd=noticeDetail&")
						.addParams("notice_group", "PUR")
						.addParams("notice_type", notice_type)
						.addParams("id", id)
						.build()
						.execute();
				
				result = okHttpResponse.string();
				if (result.contains("<script>window.location.href='https://www.cuecp.cn/")) {
					System.err.println("爬取过于频繁！" + Thread.currentThread().getName());
					result = spiderQccBidNotice(notice_type,id);
				}
				if(!StringUtils.isNullOrEmpty(result)){
					return result;
				}
			} catch (SocketTimeoutException e) {
				System.err.println("===连接超时，重新调用spiderQccBidUrl(biddingTitleUrl)方法===");
				result = spiderQccBidNotice(notice_type,id);
			} catch (Exception e) {
				e.printStackTrace();
				System.err.println("===其他异常，重新调用spiderQccBidUrl(biddingTitleUrl)方法===");
				result = spiderQccBidNotice(notice_type,id);
			}
			return result;
			
		}
		
		private String parseHtml(String qccBiddingHtml,String notice_type) {
			Document doc = Jsoup.parse(qccBiddingHtml);
			String html1= doc.getElementsByClass("subDisplay500").html();
			String id = "";
			if(html1.indexOf(notice_type) != -1 && html1.indexOf("')\"") != -1) {
				id = html1.substring(html1.indexOf(notice_type + "','") + notice_type.length()+3, html1.indexOf("')\""));
			}
			return id;
		}
}




